package utils;

import java.io.BufferedReader;
import java.io.FilterReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class TranscationReaderTestnet extends FilterReader {

    public TranscationReaderTestnet(BufferedReader br) {
        super(br);
    }

    public String[] readTx() throws IOException {
        String[] line;
        String buf;

        StringTokenizer st;
        BufferedReader br = (BufferedReader) in;
        buf = br.readLine();
        while (buf != null) {
            st = new StringTokenizer(buf, " ");
            //区分是生成交易，还是监听到的转发
            //Dec 28 01:54:49.177 (39153550) [INFO]: r:itx:c32add8cf4606ecc909d351aaecc850c3a56ba0aeb7252abc81b4c2132dcc642,143.198.218.133:18333.1
            if(buf.contains(" [INFO]: r:itx:") ){ //此行是交易
                if(st.countTokens()!=6){
                    continue;
                }
                line = new String[4];
                line[0]="r:itx";
                st.nextToken();//跳过月份
                st.nextToken();//跳过日期
                st.nextToken();//跳过时间
                line[1]=st.nextToken().replace("(","").replace(")","");//时间戳
                st.nextToken();//跳过[INFO]
                String str=st.nextToken().replace("r:itx:","");
                StringTokenizer st1=new StringTokenizer(str, ",");
                line[2]=st1.nextToken();
                line[3]=st1.nextToken();
            }else if(buf.contains(") [INFO]: r:addr:")) {
                //Dec 28 01:54:26.189 (39130563) [INFO]: r:addr:216.150.155.151,18333,1640656465,202.187.112.180:18333.3
                if(st.countTokens()!=6){
                    continue;
                }
                line = new String[5];
                line[0]="r:addr";
                st.nextToken();//跳过月份
                st.nextToken();//跳过日期
                st.nextToken();//跳过时间
                line[1]=st.nextToken().replace("(","").replace(")","");//时间戳
                st.nextToken();//跳过[INFO]
                String str=st.nextToken().replace("r:addr:","");//获得后半部分
                StringTokenizer st1=new StringTokenizer(str, ",");//逗号分割了四个
                line[2] = st.nextToken();
                line[3] = st.nextToken();
                st.nextToken();
                line[4] = st.nextToken();
            }else{
                buf = br.readLine();
                continue;
            }
            return line;
        }
        return null;
    }
}

